﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Linq;
using System.Diagnostics;
using System.Threading;

namespace fm2
{
	public class SettingsForm : Form
	{
		public DataGridView dgv;
		public CheckBox checkBoxAlwaysOnTop;
		public CheckBox checkBoxFloatUp;

		public SettingsForm()
		{
			StartPosition = FormStartPosition.CenterParent;
			MinimumSize = Size = new Size(350, 300);
			Padding = new Padding(5);
			Text = "Settings";
			KeyPreview = true;

			checkBoxAlwaysOnTop = new CheckBox();
			checkBoxAlwaysOnTop.Text = "Always on top";

			checkBoxFloatUp = new CheckBox();
			checkBoxFloatUp.Text = "Float up";
			checkBoxFloatUp.Left = checkBoxAlwaysOnTop.Width + 10;

			Panel panel1 = new Panel();
			panel1.Dock = DockStyle.Top;
			panel1.Height = checkBoxFloatUp.Height;
			panel1.Controls.AddRange( new Control [] { checkBoxAlwaysOnTop, checkBoxFloatUp } );

			Button buttonAdd = new Button();
			buttonAdd.Text = "Add...";
			buttonAdd.Click += delegate {
				FolderBrowserDialog folderDialog = new FolderBrowserDialog();
				if (folderDialog.ShowDialog() == DialogResult.OK) {
					dgv.Rows.Add(folderDialog.SelectedPath);
				}
			};
			
			Button buttonRemove = new Button();
			buttonRemove.Text = "Remove";
			buttonRemove.Click += delegate {
				var rows = (from DataGridViewRow row in dgv.Rows where row.Selected select row).ToArray();
				foreach(var row in rows) {
					dgv.Rows.Remove(row);
				}
			};
			
			Button buttonSave = new Button();
			buttonSave.Text = "Save";
			buttonSave.Click += (o,a) => DialogResult = DialogResult.OK;

			buttonAdd.Top = buttonRemove.Top = buttonSave.Top = 5;
			buttonAdd.Width = buttonRemove.Width = buttonSave.Width = 60;
			buttonRemove.Left = buttonAdd.Left + buttonAdd.Width + 10;
			buttonSave.Left = buttonRemove.Left + buttonRemove.Width + 10;
			
			Panel panel2 = new Panel();
			panel2.Height = 30;
			panel2.Anchor = AnchorStyles.Top;
			panel2.Controls.AddRange( new Control [] { buttonAdd, buttonRemove, buttonSave } );

			Panel panel3 = new Panel();
			panel3.Dock = DockStyle.Bottom;
			panel3.Height = 30;
			panel3.Controls.Add( panel2 );

			dgv = new DataGridView();
			dgv.Columns.Add("colFolders", "Folders");
			dgv.Dock = DockStyle.Fill;
			dgv.RowHeadersWidth = 24;
			dgv.AllowUserToAddRows = false;
			dgv.AllowUserToDeleteRows = false;
			dgv.AllowUserToResizeRows = false;
			dgv.AllowUserToResizeColumns = false;
			dgv.RowHeadersVisible = false;
			dgv.RowTemplate.Height = 18;
			dgv.ReadOnly = true;
			dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

			Controls.AddRange( new Control [] { dgv, panel1, panel3 } );

			Resize += (o,a) => dgv.Columns[0].Width = dgv.Width - 25;
			KeyDown += delegate(object sender, KeyEventArgs e) {
				if (e.KeyCode == Keys.Escape) DialogResult = DialogResult.Cancel;
			};
			OnResize(null);
			
			this.VisibleChanged += FromVisibleChanged;
		}
		void FromVisibleChanged(object sender, EventArgs e)
		{
			Debug.WriteLine("thread:"+Thread.CurrentThread.ManagedThreadId);//+" form:"+(UInt32)this.ParentForm.Handle) ;
		}
	}
}
